package com.hospital.hospitalsystem.modules.user.biz;

import com.github.pagehelper.PageHelper;
import com.hospital.hospitalsystem.base.PageData;
import com.hospital.hospitalsystem.modules.user.dao.entity.ShoppingCartEntity;
import com.hospital.hospitalsystem.modules.user.dto.BuyAndDeleteReq;
import com.hospital.hospitalsystem.modules.user.dto.ShoppingCartListReq;
import com.hospital.hospitalsystem.modules.user.dto.ShoppingCartReq;
import com.hospital.hospitalsystem.modules.user.dto.ShoppingCartRsp;
import com.hospital.hospitalsystem.modules.user.service.ShoppingCartService;
import com.hospital.hospitalsystem.utils.PageTransUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author qiancheng@cqxyy.net
 * @date 2023/4/1
 */
@Service
public class ShoppingCartBiz {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品进购物车
     * @param req
     */
    @Transactional
    public void addMedical(ShoppingCartReq req) {
        Example exampleShoppingCart = new Example(ShoppingCartEntity.class);
        Example.Criteria criteriaShoppingCart = exampleShoppingCart.createCriteria();
        criteriaShoppingCart.andEqualTo("userUuid",req.getUserUuid());
        criteriaShoppingCart.andEqualTo("hospitalUuid",req.getHospitalUuid());
        criteriaShoppingCart.andEqualTo("medicalUuid",req.getMedicalUuid());
        List<ShoppingCartEntity> list = shoppingCartService.selectByExample(exampleShoppingCart);
        ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
        if (CollectionUtils.isEmpty(list)) {
            BeanUtils.copyProperties(req,shoppingCartEntity);
            shoppingCartService.insertSelective(shoppingCartEntity);
        } else {
            shoppingCartEntity.setCount(list.get(0).getCount() + req.getCount());
            shoppingCartService.updateByExampleSelective(shoppingCartEntity,exampleShoppingCart);
        }
    }

    /**
     * 更改商品数量
     * @param req
     */
    public void changeMedical(ShoppingCartReq req) {
        Example exampleShoppingCart = new Example(ShoppingCartEntity.class);
        Example.Criteria criteriaShoppingCart = exampleShoppingCart.createCriteria();
        criteriaShoppingCart.andEqualTo("userUuid",req.getUserUuid());
        criteriaShoppingCart.andEqualTo("medicalUuid",req.getMedicalUuid());
        ShoppingCartEntity shoppingCartEntity = new ShoppingCartEntity();
        shoppingCartEntity.setCount(req.getCount());
        shoppingCartService.updateByExampleSelective(shoppingCartEntity,exampleShoppingCart);
    }

    /**
     * 用户删除购物车商品
     * @param req
     */
    public void deleteMedical(BuyAndDeleteReq req) {

        shoppingCartService.deleteByShoppingExt(req);
    }

    /**
     * 用户获取购物车列表
     * @param req
     * @return
     */
    public PageData<ShoppingCartRsp> getMedicalList(ShoppingCartListReq req) {
        com.github.pagehelper.Page<ShoppingCartRsp> page = PageHelper.startPage(
                req.getCurrentPage(), req.getPageSize()
        );
        shoppingCartService.getMedicalList(req);
        return PageTransUtil.createPageList(page, ShoppingCartRsp.class);
    }
}
